<!--
CodeBase: http://code.google.com/p/abhinavsingh/
Author: Abhinav Singh
Website: http://abhinavsingh.com
Blog: http://abhinavsingh.com/blog
Email: admin@abhinavsingh.com
-->
<?php

/**
 * Registration actions.
 *
 * @package    Authentication
 * @subpackage Registration
 * @author     Abhinav Singh
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class RegistrationActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex() {
    // Get the passed hidden form value
    $Perform = $this->getRequest()->getParameter('perform');
	if($Perform == "Register") {
	  // If hidden form value == "Register" , perform registration. Grab all the passed form fields
	  $UserName = $this->getRequest()->getParameter('username');
	  $EmailId = $this->getRequest()->getParameter('emailid');
	  $Password = $this->getRequest()->getParameter('password');
	  $Confirm = $this->getRequest()->getParameter('confirm');
	  
	  // Validate form
	  if($UserName == "" || $EmailId == "" || $Password == "" || $Confirm == "") {
	    // Redirect to regisration form and display error in the form ($this->redirect()) is custom symfony method to perform url redirections)
		$this->redirect('Registration/index?error=blankFields');
	  }
	  else if($Password != $Confirm) {
	    // Redirect to registration form and tell password doesn't match ($this->redirect()) is custom symfony method to perform url redirections)
		$this->redirect('Registration/index?error=passwordDontMatch');
	  }
	  else {
	    // Perform Registration, create a new users object
		// Refer C:\Workspace\Symfony\Authentication\lib\model\om\BaseUsers.php and 
		// C:\Workspace\Symfony\Authentication\lib\model\om\BaseUsersPeer.php for reference
		$newUser = new Users();
		$newUser->setUsername($UserName);
		$newUser->setEmailid($EmailId);
		$newUser->setPassword(md5($Password));
		$newUser->save();
		
		// Get Id of the user just inserted
		$UserId = $newUser->getId();
		if($UserId) {
		  // Set browser cookies
		  setCookie("ULI",$EmailId);
		  setCookie("ULK",md5($Password));
		  // Redirect to main site page
		  $this->redirect('Registration/main');
		}
		else {
		  $this->redirect('Registration/index?error=databaseError');
		}
		$this->redirect('Registration/main');
	  }
	}
  }
  
  public function executeMain() {
    $ULI = $this->getRequest()->getCookie('ULI');
    $ULK = $this->getRequest()->getCookie('ULK');
	
	// Before we allow the user to view this page
	// We need to authenticate user with the database
	$c = new Criteria();
	$c->add(UsersPeer::EMAILID,$ULI);
	$c->add(UsersPeer::PASSWORD,$ULK);
	$userInfo = UsersPeer::doSelect($c);
	if(count($userInfo) != 1) {
	  $this->redirect('Registration/index?error=authError');
	}
  }
}
